package com.example.blank.servlet;

import java.io.IOException;
import java.util.HashMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.example.blank.Dao.StudentDao;

public class student_login extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public student_login() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//http://localhost:8080/StudentChooseCourse/servlet/student_login?student_id=2014214294&student_pwd=123456
		String id = request.getParameter("student_id");
		String pwd = request.getParameter("student_pwd");
		StudentDao.testSQL(request, response);
		
		boolean b = StudentDao.judgeStudentLogin(id, pwd);
//		JSONArray array = new JSONArray();
		
		JSONObject json = new JSONObject();
		json.put("login_state", b);
		if(b)
		{
			HashMap map = StudentDao.getUserDetail(id, pwd);
			//response.getWriter().write(map.toString());
			if(map!=null)
			{
				json.put("student_name", map.get("student_name"));
				json.put("student_id", map.get("student_id"));
				json.put("student_pwd",map.get("student_pwd"));
				json.put("student_message",map.get("student_message"));
				json.put("person_id", map.get("person_id"));
			}
		}
		
		response.getWriter().write(json.toString());
		
		
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
